package com.example.xwadmin.config;

import com.example.xwadmin.api.interceptor.AllowInterceptor;
import com.example.xwadmin.api.interceptor.JwtInterceptor;
import com.example.xwadmin.api.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new JwtInterceptor()).addPathPatterns("/api/admin/**").order(1);
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/api/admin/**").order(2);
        registry.addInterceptor(new AllowInterceptor()).addPathPatterns("/api/admin/**").order(3);
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD")
                .allowCredentials(true)
                .allowedHeaders("*")
                .exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Methods", "Access-Control-Allow-Headers")
                .maxAge(3600);
    }
}
